// ref https://github.com/WICG/EventListenerOptions/pull/30
function isPassive() {
    var supportsPassiveOption = false;
    try {
        addEventListener("test", null, Object.defineProperty({}, 'passive', {
            get: function () {
                supportsPassiveOption = true;
            }
        }));
    } catch(e) {}
    return supportsPassiveOption;
}

/* face_compare  begin*/
//人脸比对变量集合
var face_compare_obj = {leftFaceList:[],rightFaceList:[],leftFaceObj:null,rightFaceObj:null,leftScroll:0,rightScroll:0};
//初始化
$(function () {
    //获取集合
    faceList();

});
var left_wrapper,right_wrapper;
function loaded () {
    left_wrapper = new IScroll('#left_wrapper', { scrollX: true, scrollY: false, mouseWheel: true });
    right_wrapper = new IScroll('#right_wrapper', { scrollX: true, scrollY: false, mouseWheel: true });
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, isPassive() ? {
    capture: false,
    passive: false
} : false);

/**
 * 图片选中
 * @param domObj 当前选中图片对象
 */
function selectFaceImg(domObj,currentIndex){
    //给当前选中图片加上边框样式
    $(domObj).addClass('active');
    $(domObj).siblings().removeClass('active');
    //获取父级ID--判断选中哪边图片对象
    var currentImgList = [];
    console.log($(domObj).parent()[0].id);
    var parentId = $(domObj).parent()[0].id;
    if(parentId == "face_left_ul"){  //选中左边
        currentImgList = face_compare_obj.leftFaceList;
        setShowImg('leftImg',currentImgList[currentIndex].faceImagePath);
        face_compare_obj.leftFaceObj = currentImgList[currentIndex];
    }else{  //选中右边
        currentImgList = face_compare_obj.rightFaceList;
        setShowImg('rightImg',currentImgList[currentIndex].faceImagePath);
        face_compare_obj.rightFaceObj = currentImgList[currentIndex];
    }
    //人脸比对
    faceCompare();
}
/**
 * 界面li组装填充
 * @param direction  方向
 * @param arrData  数据列表
 */
function liControll(direction,arrData){
    var li_content = "";
    if(arrData.length>0){
        if(arrData.length>0){
            for(var i=0;i<arrData.length;i++){
                var temp_obj = arrData[i];
                if(i==0){
                    li_content+='<li class="flex active" id="'+temp_obj.imgId+'" data-index="'+i+'"  onclick="selectFaceImg(this,'+i+');"><img src="'+temp_obj.faceImagePath+'"/></li>';
                }else{
                    li_content+='<li class="flex" id="'+temp_obj.imgId+'" data-index="'+i+'" onclick="selectFaceImg(this,'+i+');"><img src="'+temp_obj.faceImagePath+'"/></li>';
                }
            }
        }
    }
    if(direction=="all"){
        //先清空ul中内容
        $("#face_left_ul").html("");
        $("#face_right_ul").html("");
        //设置内容
        $("#face_left_ul").append(li_content);
        $("#face_right_ul").append(li_content);
    }else if(direction=="left"){
        $("#face_left_ul").html("");
        $("#face_left_ul").append(li_content);
    }else if(direction=="right"){
        $("#face_right_ul").html("");
        $("#face_right_ul").append(li_content);
    }
    //滑动
    window.setTimeout(function(){
        loaded();
    },1000);
}

/**
 * 获取初始化人脸对比图片列表数据
 */
function faceList(){
    $("#face_left_ul").html("");
    $("#face_right_ul").html("");
    setLoadingImg('leftImg');
    setLoadingImg('rightImg');
    $.get('http://10.10.23.163:8080/face/compare/lists', {},function(data) {
        console.log(data);
        if(data.errorCode){
            systemTips(data.errorMessage);
        }else {
           console.log(data);
           var li_content = "";
           if(data.length>0){
              $("#left_scroller,#right_scroller").css('width',(data.length*175)+'px');
               //设置左右两边图片集合
               face_compare_obj.leftFaceList=data.concat();
               face_compare_obj.rightFaceList=data.concat();
               console.log("初始化列表:"+face_compare_obj.leftFaceList.length);
               //需要设置初始化
               face_compare_obj.rightFaceObj=face_compare_obj.leftFaceObj = data[0];
               setShowImg('leftImg',face_compare_obj.leftFaceObj.faceImagePath);
               setShowImg('rightImg',face_compare_obj.rightFaceObj.faceImagePath);
               //人脸比对
               faceCompare();
               //界面li组装填充
               liControll("all",data);
           }else{
               systemTips("列表数据为空");
           }
        }
    },"json");
}
/**
 * 本地上传 加上loading图片事件
 * @param imageId loading图片存放路径
 */
function changeImge(imageId){
    $("#"+imageId).attr("src","./images/loading.gif");
}
/**
 * 显示loading图片
 * @param id
 */
function setLoadingImg(id){
  var _img = '<img src="images/loading.gif" class="img2" />';
  $("#"+id).html('');
  $("#"+id).append(_img);
}
/**
 * 显示指定图片
 * @param id
 * @param imgpath
 */
function setShowImg(id,imgpath){
  var _img = '<img src="'+imgpath+'" class="img2" />';
  $("#"+id).html('');
  $("#"+id).append(_img);
}
/**
 * 人脸对比图片上传接口
 * @param from
 * @param imageId
 */
function updataImg(from,imageId) {
    setLoadingImg(imageId);
    //控制图片上传中全频提示显示，隐藏
    console.log("updataImg---"+new Date().getMinutes()+"--"+new Date().getSeconds());
    var form = new FormData(document.getElementById(from));
    $.ajax({
        url: "http://10.10.23.163:8080/face/compare/uploadFile",
        type: "post",
        data: form,
        cache: false,
        processData: false,
        contentType: false,
        async: true,
        success: function (data) {
            console.log("updataImg-success--"+new Date().getMinutes()+"--"+new Date().getSeconds());
            if(data.errorCode){
                systemTips(data.errorMessage,imageId);
            }else{
                console.log(data);
                setShowImg(imageId,data.faceImagePath);
                // $("#"+imageId).attr("src", data.faceImagePath);
                //设置人脸对比数据
                if(imageId == "rightImg"){
                    face_compare_obj.rightFaceObj = data;
                    face_compare_obj.rightFaceList.unshift(data);
                    //界面li组装填充
                    liControll("right",face_compare_obj.rightFaceList);
                }else{
                    face_compare_obj.leftFaceObj = data;
                    face_compare_obj.leftFaceList.unshift(data);
                    //界面li组装填充
                    liControll("left",face_compare_obj.leftFaceList);
                }
                //人脸比对
                faceCompare();
            }
        },
        error: function (e) {
            console.log("updataImg-error--"+new Date().getMinutes()+"--"+new Date().getSeconds());
            console.log("上传错误...");
            systemTips("图片上传错误",imageId);
        }
    });
}


/**
 * 人脸比对
 */
function faceCompare(){
    $("#take_time_class").html("");
  if(face_compare_obj.rightFaceObj!=null && face_compare_obj.leftFaceObj != null){
      var param = {"imgId1":face_compare_obj.leftFaceObj.imgId,"imgId2":face_compare_obj.rightFaceObj.imgId};
      console.log(param);
      $.post('http://10.10.23.163:8080/face/compare/img', param,function(data) {
          console.log(data);
          if(data.errorCode){
              systemTips(data.errorMessage);
          }else{
              console.log(data);
              var degree = (data.score*100).toFixed(2);
              $("#degree").html( degree+"%");
              $("#take_time_class").append('<span style="margin-right:20px;">take</span><span style="font-weight: 700;">'+data.times+'</span> ms');
          }
      },"json");
  }else{
      systemTips("请选择人脸对比图片");
  }
}
/**
 * url地址上传图片
 * @param inputId  图片上传ID
 */
function urlUploadImg(inputId,imageId){
    setLoadingImg(imageId);
    // $("#"+imageId).attr("src","images/loading.gif");
    if(inputId){
       console.log($("#"+inputId).val());
        if($("#"+inputId).val()!=""){
            var param = {"imagePath":$("#"+inputId).val()};
            $.post('http://10.10.23.163:8080/face/compare/uploadUrl', param,function(data) {
                console.log(data);
                if(data.errorCode){
                    systemTips(data.errorMessage,imageId);
                }else{
                    //{imgId: "f245ff74f391eaecb170b7c0b35e6b0d", faceImagePath: "http://10.10.1.54:18989/M00/51/02/CgoBNlnLX0eAFlwIAABnAejdk20098.jpg"}
                    // $("#"+imageId).attr("src",data.faceImagePath);
                    setShowImg(imageId,data.faceImagePath);
                    if(inputId == "right_input"){
                        face_compare_obj.rightFaceObj = data;
                        face_compare_obj.rightFaceList.unshift(data);

                        //界面li组装填充
                        liControll("right",face_compare_obj.rightFaceList);
                    }else{
                        face_compare_obj.leftFaceObj = data;
                        face_compare_obj.leftFaceList.unshift(data);
                        //界面li组装填充
                        liControll("left",face_compare_obj.leftFaceList);
                    }
                    //人脸比对
                    faceCompare();
                }
            },"json");
        }else{
            systemTips("图片URL地址不能为空",imageId);
        }
    }
}

/* face_compare  end*/


/**
 * 提示信息
 * @param param  提示内容
 */
function systemTips(param,imageId) {
    if(imageId!=undefined){
        var temp_imgUrl =  face_compare_obj.rightFaceObj.faceImagePath;
        if(imageId.indexOf("left")>=0){
            temp_imgUrl =  face_compare_obj.leftFaceObj.faceImagePath;
        }
      //  $("#"+imageId).attr("src",temp_imgUrl);
        setShowImg(imageId,temp_imgUrl);
    }
    window.parent.system_tips(param);
    /*$(".systemHintClass").show();
    $(".systemHintClass")[0].innerHTML = param;
    //解绑事件
    window.setTimeout(
        function () {
            $(".systemHintClass").hide();
        }, 2000
    );*/
}
